package com.sali.utils;

import lombok.extern.slf4j.Slf4j;

/**
 * @description:
 * @author: sali
 * @date: 2025/2/21 下午11:21.
 * @version: 1.0
 */
@Slf4j
public class ByteUtil {

    private ByteUtil() {
    }

    /**
     * int 转byte
     *
     * @param value
     * @return
     */
    public static byte[] intToBytes(int value) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (value & 0xff);
        bytes[1] = (byte) (value >> 8 & 0xff);
        bytes[2] = (byte) (value >> 16 & 0xff);
        bytes[3] = (byte) (value >> 24 & 0xff);
        return bytes;
    }

    /**
     * int 转byte
     *
     * @param value
     * @return
     */
    public static byte[] intToBytes2(int value) {
        byte[] bytes = new byte[4];
        bytes[3] = (byte) (value & 0xff);
        bytes[2] = (byte) (value >> 8 & 0xff);
        bytes[1] = (byte) (value >> 16 & 0xff);
        bytes[0] = (byte) (value >> 24 & 0xff);
        return bytes;
    }

    public static int bytesToInt(byte[] bytes) {
        return ((bytes[0] & 0xff) | (bytes[1] & 0xff) << 8 | (bytes[2] & 0xff) << 16 | (bytes[3] & 0xff) << 24);
    }

}
